﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using PPI.Client.Logic;
using PPI.Entities;

namespace PPI
{
    public partial class frmLogin : DevComponents.DotNetBar.Office2007Form
    {
        private List<EUser> m_SystemUsers;

        public frmLogin()
        {
            InitializeComponent();
        }

        public EUser LoggedUser
        {
            get;
            set;
        }

        public bool IsUserLogged
        {
            get
            {
                if (LoggedUser == null)
                    return false;
                return LUser.ValidUser(m_SystemUsers,LoggedUser);
            }
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            btnLogin_Click();
        }

        private void btnLogin_Click()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(btnLogin_Click));
            }
            else
            {
                if (ValidateControls())
                {
                    DialogResult = DialogResult.OK;
                }
            }
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            btnCancel_Click();
        }

        private void btnCancel_Click()
        {
            if (InvokeRequired)
            {
                BeginInvoke(new MethodInvoker(btnCancel_Click));
            }
            else
            {
                DialogResult = DialogResult.Cancel;
            }
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            m_SystemUsers = LUser.GetAll();

            if(LoggedUser != null)
            {
                txtUsername.Text = LoggedUser.Username;
                txtPassword.Text = LoggedUser.Password;
            }
        }

        #region strings

        string m_UsernameRequired = "Username is required";
        string m_UsernameInvalid = "The supplied username is not valid";
        string m_PasswordRequired = "Password is required";
        string m_PasswordInvalid = "The supplied password is not valid";
        string m_LoginFailed = "Either the supplied username or password is incorrect";

        #endregion

        private bool ValidateControls()
        {
            // username is required

            if (string.IsNullOrEmpty(txtUsername.Text))
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(m_UsernameRequired);
                return false;
            }

            // password is required

            if (string.IsNullOrEmpty(txtPassword.Text))
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(m_PasswordRequired);
                return false;
            }

            // validate credentials

            LoggedUser = new EUser(txtUsername.Text, txtPassword.Text);
            if (!LUser.ValidUser(m_SystemUsers,LoggedUser))
            {
                DevComponents.DotNetBar.MessageBoxEx.Show(m_LoginFailed);
                return false;
            }

            // if we get this far, then we "logged in" okay

            LoggedUser = m_SystemUsers.Find(delegate(EUser u)
            {
                return
                    (
                        u.Username.ToUpper() == txtUsername.Text.ToUpper() &&
                        u.Password.ToUpper() == txtPassword.Text.ToUpper()
                    );

            });

            if(LoggedUser == null)
                return false;

            return true;
        }

        private void txtPassword_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnLogin_Click(null, null);
        }
    }
}
